/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package our.gui;

import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.KeyStroke;

/**
 *
 * @author keysman
 */
public class ourCheckBox extends javax.swing.JCheckBox implements ourInterfaceCommonObj {
    // Definisco la classe che possiede le funzioni comuni a tutti gli oggetti a video
    public our.gui.commonObjectClass SETTINGS = new our.gui.commonObjectClass();
    private java.lang.String _FieldOnDB = "";    // Il corrispondente nome del campo su db
    private java.lang.String _HelpMsg = "";      // Il messaggio che verrà visualizzato sulla StatusBar
    private boolean _FirstFieldOutOfKeyFrame = false; // Se è il primo elemento fuori dal keyframe

    public boolean isFirstFieldOutOfKeyFrame() {
        return _FirstFieldOutOfKeyFrame;
    }

    public void setFirstFieldOutOfKeyFrame(boolean _FirstFieldOutOfKeyFrame) {
        this._FirstFieldOutOfKeyFrame = _FirstFieldOutOfKeyFrame;
    }

    public String getHelpMsg() {
        return _HelpMsg;
    }

    public void setHelpMsg(String _HelpMsg) {
        this._HelpMsg = _HelpMsg;
    }
    public String getFieldOnDB() {
        return _FieldOnDB;
    }

    public void setFieldOnDB(String _FieldOnDB) {
        this._FieldOnDB = _FieldOnDB;
    }

    public void init()
    {
        // Creo una nuova etichetta
        our.gui.ourLabel lbl = new our.gui.ourLabel();
        lbl.setText(getText());
        java.awt.Rectangle rct = getBounds();
        this.setBounds(rct.x, rct.y, 20, rct.height);
        //rct.y = 0;
        rct.x += 21;
        lbl.setBounds(rct);
        getParent().add(lbl);
    }
    
    public ourCheckBox()
    {
        super();
        // Imposto il messaggio di help
        SETTINGS.setHelpMsg(getHelpMsg());
        // Disabilito l'oggetto per default
        setEnabled(false);
        // Imposto il font
        setFont(SETTINGS.getLabelFont());
        // Imposto l'altezza dell'oggetto
        setSize(getWidth(), SETTINGS.getOurHeight());
        getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("DOWN"), "down");
        getActionMap().put("down", new AbstractAction(){  
            @Override
            public void actionPerformed(ActionEvent evt) {    
                KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
                kfm.focusNextComponent();
            }
        });
        
        getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("ENTER"), "enter");
        getActionMap().put("enter", new AbstractAction(){  
            @Override
            public void actionPerformed(ActionEvent evt) {    
                KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
                kfm.focusNextComponent();
            }
        });
        
        getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("UP"), "up");
        getActionMap().put("up", new AbstractAction(){  
            @Override
            public void actionPerformed(ActionEvent evt) {
                KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
                kfm.focusPreviousComponent();
            }
        });
    }
    
    /*@Override
    public void setEnabled(boolean b)
    {
        super.setFocusable(b);
    }*/



    /**
     * Questa funzione disabilita la gestione del mouse del campo ourTextField e in caso di MOUSE_CLICKED richiama
     * la funzione di gestione del parent: ourTextField
     * @param e
     */
    @Override
    protected void processMouseEvent(java.awt.event.MouseEvent e)
    {
        boolean bConsumeEvent = true;
        if(SETTINGS.getOurForm()!=null)
        {
            if(!(isEnabled()))
            {
                e.consume();
                return;
            }

            if(e.paramString().indexOf("MOUSE_CLICKED")>=0)
            {
                bConsumeEvent = !(SETTINGS.getOurForm().clickOnComponent(this));
                //if(isFocusable()) setSelected(!isSelected());
                if(isEnabled()) setSelected(!isSelected());
            }
            if(bConsumeEvent)
            {
                e.consume();
            } else {
                requestFocus();
                super.processMouseEvent(e);
            }
        } else if(e.paramString().indexOf("MOUSE_CLICKED")>=0) {
            requestFocus();
            super.processMouseEvent(e);
        }
    }


    public boolean ISVALID(boolean bShowErrorMessage) {
        return true;
    }

    public void reset()
    {
        setSelected(false);
    }

}
